/** 
 * ================================================================================
 * 		Project		: Kompas Bola
 * 		File		: TabInfo.java
 * 		Description	: Tab Info UI
 * 		Called		: AbstractTabStackNavigation.java
 * ================================================================================
 * */
package com.kompas.bola.application.ui;

import java.util.Stack;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.kompas.bola.application.R;

public class TabInfo implements ActionBar.TabListener {
	/** =========================================================================== */

  Stack<Fragment> stack 								= new Stack<Fragment>();
  Tab tab;
  private final AbstractTabStackNavigation tabStackNavigation;
  Integer tag;
	/** =========================================================================== */
  TabInfo(final AbstractTabStackNavigation tabStackNavigation) {
    this.tabStackNavigation 							= tabStackNavigation;
  }
	/** =========================================================================== */
  void attachFragment(final FragmentTransaction transaction, final Fragment fragment) {
    if (tabStackNavigation.fragmentWasAdded.containsKey(fragment)) {
      transaction.attach(fragment);
    } else {
      tabStackNavigation.fragmentWasAdded.put(fragment, Boolean.TRUE);
      transaction.add(R.id.fragment_container, fragment, String.valueOf(tag));
    }
    tabStackNavigation.setUpButtonDependingStack();
  }
	/** =========================================================================== */
  void detachFragment(final FragmentTransaction transaction, final Fragment fragment) {
    if (!fragment.isDetached()) {
      transaction.detach(fragment);
    }
    tabStackNavigation.setUpButtonDependingStack();
  }
	/** =========================================================================== */
  @Override
  public void onTabReselected(final Tab tab, final FragmentTransaction transaction) {
  }
	/** =========================================================================== */
  @Override
  public void onTabSelected(final Tab tab, final FragmentTransaction transaction) {
    attachFragment(transaction, stack.peek());
  }
	/** =========================================================================== */
  @Override
  public void onTabUnselected(final Tab tab, final FragmentTransaction transaction) {
    detachFragment(transaction, stack.peek());
  }
	/** =========================================================================== */
 }